﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
//using Framework.ServerLog;
using Entities;
using DAL;

using System.Collections.Generic;

public partial class Monitor : System.Web.UI.Page
{
    public string ConnectionString = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"];

    protected void Page_Load(object sender, EventArgs e)
    {

        if (Request.QueryString["ErrorInfo"] != null && Request.QueryString["Flag"].ToLower() != null)
        {
            string usergroupstr = "";
            int groupid = 0;
            if (Request.QueryString["UserGroup"] != null)
            {
                usergroupstr = HttpUtility.UrlDecode(Request.QueryString["UserGroup"].Trim());
                DAL.GroupDALs groupDals = new DAL.GroupDALs();
                groupid = groupDals.GetByName(usergroupstr);
            }
            //System.Text.Encoding.GetEncoding(936) GBK
            //System.Text.Encoding.UTF8 UTF8
            string errorinfo = HttpUtility.UrlDecode(Request.QueryString["ErrorInfo"]);
            string flag = Request.QueryString["Flag"].ToLower();

            Entities.CheckPoint result = GetCheckPoint(flag, groupid);

            string sql = @" insert into CheckLog(CheckPointID,Status,ErrorInfo) values(" + result.CheckPointID + "," + 0 + ",'" + errorinfo + "')";
                         
            System.Data.SqlClient.SqlConnection myConnection = new System.Data.SqlClient.SqlConnection(ConnectionString);

            System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand(sql, myConnection);


            myConnection.Open();


            try
            {
                command.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                throw new Exception(ex.Message + ex.StackTrace);
            }
            catch (Exception ex1)
            {
                throw new Exception(ex1.Message + ex1.StackTrace);
            }
            finally
            {
                command.Dispose();
                myConnection.Close();
            }
        }
   
    }




    public Entities.CheckPoint GetCheckPoint(string flag, int groupId)
    {
        System.Data.SqlClient.SqlConnection myConnection = new System.Data.SqlClient.SqlConnection(ConnectionString);
        string sql = @"select * from [CheckPoint] where F1 = '" + flag + "' and GroupID = " + groupId + " order by checkintime desc " ;
        Entities.CheckPoint result = new Entities.CheckPoint();
        System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand(sql, myConnection);

        myConnection.Open();
        SqlDataReader dr = command.ExecuteReader();
        if (dr.Read())
        {
            result = PopulateCheckPointFromIDataReader(dr);
            dr.Close();
        }
        command.Dispose();
        myConnection.Close();
        return result;

    }

    public Entities.CheckPoint PopulateCheckPointFromIDataReader(IDataReader dr)
    {
        Entities.CheckPoint checkPoint = new Entities.CheckPoint();
        try
        {
            checkPoint.CheckPointID = (dr["CheckPointID"] == System.DBNull.Value ? 0 : Convert.ToInt32(dr["CheckPointID"]));
            checkPoint.IsSendSMS = (dr["IsSendSMS"] == System.DBNull.Value ? false : Convert.ToBoolean(dr["IsSendSMS"]));
            checkPoint.IsSendEmail = (dr["IsSendEmail"] == System.DBNull.Value ? false : Convert.ToBoolean(dr["IsSendEmail"]));
            checkPoint.Status = (dr["Status"] == System.DBNull.Value ? (short)0 : Convert.ToInt16(dr["Status"]));
            checkPoint.SendMessage = (dr["SendMessage"] == System.DBNull.Value ? null : Convert.ToString(dr["SendMessage"]));
            checkPoint.CheckValue = (dr["CheckValue"] == System.DBNull.Value ? null : Convert.ToString(dr["CheckValue"]));
            checkPoint.Periods = (dr["Periods"] == System.DBNull.Value ? (int?)null : Convert.ToInt32(dr["Periods"]));
            checkPoint.StartTime = (dr["StartTime"] == System.DBNull.Value ? null : Convert.ToString(dr["StartTime"]));
            checkPoint.LogNum = (dr["LogNum"] == System.DBNull.Value ? (int?)null : Convert.ToInt32(dr["LogNum"]));
            checkPoint.EndTime = (dr["EndTime"] == System.DBNull.Value ? null : Convert.ToString(dr["EndTime"]));
            checkPoint.CheckInTime = (dr["CheckInTime"] == System.DBNull.Value ? DateTime.Parse("1900-01-01") : Convert.ToDateTime(dr["CheckInTime"]));
            checkPoint.GroupID = (dr["GroupID"] == System.DBNull.Value ? 0 : Convert.ToInt32(dr["GroupID"]));
            checkPoint.F1 = (dr["F1"] == System.DBNull.Value ? null : Convert.ToString(dr["F1"]));
            checkPoint.F2 = (dr["F2"] == System.DBNull.Value ? null : Convert.ToString(dr["F2"]));
            checkPoint.F3 = (dr["F3"] == System.DBNull.Value ? null : Convert.ToString(dr["F3"]));
            checkPoint.F4 = (dr["F4"] == System.DBNull.Value ? null : Convert.ToString(dr["F4"]));
            checkPoint.F5 = (dr["F5"] == System.DBNull.Value ? null : Convert.ToString(dr["F5"]));
            checkPoint.F6 = (dr["F6"] == System.DBNull.Value ? null : Convert.ToString(dr["F6"]));
        }
        catch (Exception ex)
        {
            throw new Exception("从DataReader中读取数据失败，可能与相关的字段不存在有关！请检查！||" + ex.Message + "||" + ex.Source);
        }
        return checkPoint;
    }
}
